home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 11660 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.0 KB  |  56 lines

  1. Newsgroups: comp.lang.c
  2. Path: bath.ac.uk!bsmail!talisker!nathan
  3. From: nathan@pact.srf.ac.uk (Nathan Sidwell)
  4. Subject: Re: 16-bit memset?
  5. Message-ID: <Dou51s.90p@uns.bris.ac.uk>
  6. Sender: usenet@uns.bris.ac.uk (Usenet news owner)
  7. Nntp-Posting-Host: talisker.pact.srf.ac.uk
  8. Organization: Inmos
  9. X-Newsreader: TIN [version 1.2 PL2]
  10. References: <joules-1803962243060001@badboy.mit.edu> <4ill6r$qnm@nntp.interaccess.com> <DoJABC.Hy7@iquest.net>
  11. Date: Mon, 25 Mar 1996 18:08:16 GMT
  12.  
  13. Doug Miller (dlmiller@iquest.net) wrote:
  14. : +Julian Orbanes wrote:
  15. : +>I am looking quick way to set a series of 16-bit values to one value.
  16. : +>
  17. : +>Analagous to how memset set works with 8-bit values. (For graphics
  18. : +>purposes).
  19.  
  20. : char    *bigstuff;
  21. :  . .
  22. : /* assuming that:                                */
  23. : /*    1) you have allocated storage for bigstuff                */
  24. : /*    2) it is a null-terminated string of the desired FINAL length            */
  25. I don't understand this assumption, strncpy copies until either the number
  26. of characters has been copied OR a NUL char has been copied.
  27. : /*    3) you have loaded the value you wish to propagate into the first 2 bytes    */
  28.  
  29. : strncpy (bigstuff + 2, bigstuff, (strlen (bigstuff) - 2));
  30.                                     ^^^^^^ this relies on bugstuff already
  31. being initialized!
  32.  
  33. BUT more importantly, the src and dest locations of all the str* functions
  34. must not overlap -- if they do, it's undefined behaviour.
  35.  
  36. One way to do what Julian wanted is to do something like
  37. WARNING untested C code off the top of my head
  38. void init16(Int16 *buffer, size_t num, Int16 val)
  39. {
  40.   size_t count;
  41.  
  42.   *buffer = val;
  43.   for(count = 1; count << 1 <= num; count <<= 1)
  44.     memcpy(&buffer[count], buffer, count * sizeof(Int16));
  45.   if(count != num)
  46.     memcpy(&buffer[count], buffer, (num - count) * sizeof(Int16));
  47. }
  48.  
  49. nathan
  50.  
  51. --
  52. Nathan Sidwell                         Holder of the Xmris home page
  53. Chameleon Architecture Group at SGS-Thomson, formerly Inmos
  54. http://www.pact.srf.ac.uk/~nathan/                  Tel 0117 9707182
  55. nathan@inmos.co.uk or nathan@bristol.st.com or nathan@pact.srf.ac.uk
  56.